<?php
namespace app\wxapp\controller;

use app\model\Theme; 
use app\model\Poster as Poster_mod;

class Poster extends Base
{
    public function Poster()
    {
    global $_P, $_G;    
    $themeid = intval($_P['themeid']);
    $theme = Theme::getThemeByIdxcx($themeid);
    if(empty($theme['id'])){
        $this->result(1, '活动入口不正确'); 
        exit;
    }
    $fans = $this->checkuser();
    $uid = intval($fans['uid']);
    if($uid < 1){
        $this->result(1, '用户信息出错'); 
        exit;
    }

    $p = array(":uid"=>$fans['uid'],":themeid"=>$themeid);
    $sql = "SELECT * FROM ".tablename(t_fans)." 
            WHERE themeid = :themeid and uid = :uid";
    $myfans = db_find($sql,$p);
    if (!empty($myfans['poster']) ) {//&& ($myfans['postertime']+1296000) > TIMESTAMP
        $result['hbimg'] = $myfans['poster'] ;
        $this->result(0, '海报生成成功',$result); 
    }else{
        //若海报未设置，则只返回二维码
        $system=$_G['system'];
        $scene = MODULE_NAME . "-" . $themeid . "-" . $uid . "-" . $theme['softkey'] ;
        $qrimg = Poster_mod::CreateScene($system,$scene,'pages/index/index');

        $poster =  Poster_mod::getByThemeIdnew($themeid);
        if(!empty($poster['id'])){
            $member = array(
                "openid"   =>$fans['openid'],
                'nickname' =>$fans['nickname'],
                'avatar'   =>$fans['avatar'],
                // 'shareurl' => $shareurl,
                'themeid' =>$themeid,
                'addtime' => TIMESTAMP,
            );
            $poster['bg']=str_replace("/99.jpg","/haibao.jpg",$poster['bg']);

            $img = Poster_mod::createGoodsPoster($poster, $member, $qrimg);

            $result['hbimg'] = str_replace("http://", "https://", $img['img']) ;
            $result['qrimg'] =  str_replace("http://", "https://", $qrimg) ;
            db_update(t_fans, array('poster' =>$result['hbimg'],'postertime' =>TIMESTAMP), array('uniacid' => $_G['mpid'], 'themeid' => $themeid, 'openid' => $_G['openid']));
        }else{
            $result['qrimg'] =  str_replace("http://", "https://", $qrimg) ;
            $result['hbimg'] =  str_replace("http://", "https://", $qrimg) ;
        }
        $this->result(0, '海报生成成功',$result); 
    }
}
}